/*
 * 文件名： AdvertActivity.java
 * 描    述：首屏广告
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppHelper;
import com.miguo.bean.AdvertBean;
import com.miguo.ui.R;
import com.miguo.utils.Options;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class AdvertActivity extends BaseActivity implements OnClickListener {

    private Intent in;
    private Bundle bundle;
    private AdvertBean Advert;
    private LinearLayout mSkip;
    private LinearLayout mDetail;
    private LinearLayout mSplashCover;
    private ImageView mSplashImg;
    private boolean skip = false;
    protected ImageLoader imageLoader = ImageLoader.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advert);
        initExtra();
        findViewById();
        initView();
    }

    private void initExtra() {
        in = getIntent();
        bundle = in.getExtras();
        if (bundle == null) {
            StartMain();
        } else {
            Advert = (AdvertBean) in.getSerializableExtra("Advert");
        }
    }

    private void findViewById() {
        mSkip = (LinearLayout) findViewById(R.id.splash_skip);
        mDetail = (LinearLayout) findViewById(R.id.splash_detail);
        mSplashCover = (LinearLayout) findViewById(R.id.splash_cover);
        mSplashImg = (ImageView) findViewById(R.id.splash_start_img);
    }

    private void initView() {
        setSwipeBackEnable(false);
        imageLoader.displayImage(Advert.getFull_screen_pic_url(),
                mSplashImg, Options.getFullScreenOptions(),
                defaultImageLoadingListener);

        mSkip.setOnClickListener(this);
        mDetail.setOnClickListener(this);
        mSplashImg.setOnClickListener(this);

        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (!skip) {
                    StartMain();
                }
            }
        }, 5000);
    }

    private void StartMain() {
        skip = true;
        AppHelper.StartMain(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.splash_skip:
            /* 跳过 */
                StartMain();
                break;
            case R.id.splash_detail:
            /* 查看详情 */
            case R.id.splash_start_img:
            /* 点击广告 */
                skip = true;
                AppHelper.startActivityByType(this, Advert, false);
                this.finish();
                break;
        }
    }

    private SimpleImageLoadingListener defaultImageLoadingListener = new SimpleImageLoadingListener() {
        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                mSplashCover.setVisibility(View.VISIBLE);
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }
}
